// pages/driver/activity/activity.js
var handler = require("../../../utils/function.js"),
    api = require("../../../api.js"),
    app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        getAwardButton: true,
        award: [],
        finished: 0,
        unfinished: 0,
        list: {},
        is: 0,
        idlist: [],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {},
    // getAwardHandler 点击领取奖励事件
    getAwardHandler: function() {
        const {
            getAwardButton,
            award
        } = this.data;
        
        if (this.data.is==1) {
            var that = this;
            app.request({
                url: api.driver.keytoget, //一键领取奖励
                method: 'post',
                data: {
                    id: JSON.stringify(that.data.idlist)
                },
                success: function(rs) {
                    console.log('一键领取奖励', rs);
                    if (rs.code == 1) {
                        wx.showToast({
                            title: '领取成功',
                        })
                    }else{
                          wx.showToast({
                            title: '领取失败',
                            icon: "none"
                        })

                    }
                }
            })
        } else {
            wx.showToast({
                title: '暂无可领取奖励',
                icon: "none"
            })
        }
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        console.log('参数', e)
        wx.setStorageSync("current_award", this.data.award[e.currentTarget.dataset.id]);
        console.log(e.currentTarget.dataset.skip);
        handler.intoPageHandler(e.currentTarget.dataset.skip + '?id=' + e.currentTarget.dataset.id)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        app.request({
            url: api.driver.task,
            method: 'post',
            data: {},
            success: function(rs) {
                that.setData({
                    list: rs.data.data.data,
                    unfinished: rs.data.unfinished,
                    is: rs.data.unfinished,
                    idlist: rs.data.idlist
                })
            }
        })
        /* const order=wx.getStorageSync("order");
         var finished=0;
         var unfinished=0;
         for(var i=0;i<order.length;i++){
           finished = order[i].status === 3 ? ++finished : finished;
           unfinished = order[i].status === 7 ? ++unfinished : unfinished;
         }
         const award = wx.getStorageSync("award");
         var getAward=-1;
         for (var i = 0; i < award.length;i++){
           (!award[i].status)&& ++getAward;
         }
         console.log(getAward);
         this.setData({ award, unfinished, getAwardButton: getAward === award.length-1?false:true})*/
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        var that = this;
        wx.showNavigationBarLoading();
        app.request({
            url: api.driver.task,
            method: 'post',
            data: {},
            success: function(rs) {
                that.setData({
                    list: rs.data.data.data,
                    unfinished: rs.data.unfinished,
                    is: rs.data.unfinished,
                });
                wx.hideNavigationBarLoading()
            }
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        wx.showNavigationBarLoading();
        var p = 1;
        p = p + 1;
        app.request({
            url: api.driver.task,
            method: 'post',
            data: {
                page: p,
            },
            success: function(rs) {
                that.setData({
                    list: rs.data.data.data,
                    unfinished: rs.data.unfinished,
                    is: rs.data.unfinished,
                });
                wx.hideNavigationBarLoading()
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {}
})